//index.js
const Controllers = require('../../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    agentIndexData: {
      rentCommission: '',
      rewardState: 0,
      rewardProportion: '',
      rewardPrice: ''
    },
    isLoading: false
  },
  onLoad(option) {
  },
  onShow() {
    var that = this;
    const agentIndexData = app.globalData.agentIndexData;
    console.log('agentIndexData==', agentIndexData);
    
    if (!agentIndexData || Object.keys(agentIndexData).length === 0) {
      that.agentIndex();
    } else {
      that.setData({
        agentIndexData
      })
    }
  },
  sub(e) {
    var that = this;
    var agentIndexData = that.data.agentIndexData;
    if (that.data.isLoading) {
      return;
    }

    var showToast = false;
    var title = '';

    if (!agentIndexData.rewardState) {
      showToast = true;
      title = '请选择销售云商品提成类型';
    } else if (agentIndexData.rewardState === 1 && (agentIndexData.rewardProportion < 0 || agentIndexData.rewardProportion > 100)) {
      showToast = true;
      title = '请输入正确的销售金额比例 0～100 之间';
    } else if (agentIndexData.rewardState === 1 && (agentIndexData.rewardPrice < 0 || agentIndexData.rewardPrice > 100)) {
      showToast = true;
      title = '请输入正确的销售固定金额';
    } else if (agentIndexData.rentCommission < 0 || agentIndexData.rentCommission > 100) {
      showToast = true;
      title = '请输入正确的员工招商提成 0～100 之间';
    }
    if (showToast) {
      wx.showToast({
        title,
        icon: 'none'
      });
      return;
    }

    const params = {
      rentCommission: agentIndexData.rentCommission,
      rewardState: agentIndexData.rewardState,
      rewardProportion: agentIndexData.rewardProportion,
      rewardPrice: agentIndexData.rewardPrice
    }
    that.data.isLoading = true;
    Controllers.setStaff(params).then(res => {
      console.log('setStaff--res==', res);
      const { code, data, desc } = res;
      wx.showToast({
        title: desc,
        icon: code === 1 ? 'success' : 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      });
    })
  },
  getInput(e) {
    var that = this;
    var name = e.target.dataset.name;
    var value = e.detail.value;
    var agentIndexData = that.data.agentIndexData;

    if (name === 'rewardProportion') {
      agentIndexData.rewardState = 1;
    } else if (name === 'rewardPrice') {
      agentIndexData.rewardState = 2;
    }

    agentIndexData[name] = value;
    that.setData({
      agentIndexData
    })
    console.log('that.data.agentIndexData==', that.data.agentIndexData);
  },
  changeReward(e) {
    var that = this;
    console.log('changeReward--e==', e);
    var value = parseInt(e.detail.value);
    that.data.agentIndexData.rewardState = value;
    that.setData({
      agentIndexData: that.data.agentIndexData
    })
  },
  dataStatistics() {
    var that = this;
    Controllers.dataStatistics({}).then(res => {
      console.log('dataStatistics--res==', res);
      const { code, data, desc } = res;
      var lists = that.data.lists;
      // 资金统计》总收益；
      lists[0].childs[0].value = parseFloat(data.rent + data.cloud + data.agentStaffCommission).toFixed(2); 
      that.setData({
        lists,
        statistics: data
      })
    });
  }
})